import 'package:fate_blind_box/network/http_request.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/ui/account/profile_complete_v4_controller.dart';
import 'package:fate_blind_box/ui/account/sex_info_model.dart';
import 'package:fate_blind_box/ui/guide/download_new.dart';
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:fate_blind_box/utils/loading.dart';
import 'package:fate_blind_box/utils/page.dart';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../utils/provider.dart';
import '../../widget/date_picker/flutter_datetime_picker.dart';
import 'package:date_format/date_format.dart';

class ProfileCompleteV4Ui extends StatefulWidget {
  const ProfileCompleteV4Ui({Key? key}) : super(key: key);

  @override
  State<ProfileCompleteV4Ui> createState() => _ProfileCompleteV4UiState();
}

class _ProfileCompleteV4UiState extends State<ProfileCompleteV4Ui> {
  double imgWidth = 60;

  ProfileCompleteV4Controller completeV4Controller =
      Get.put(ProfileCompleteV4Controller());

  @override
  Widget build(BuildContext context) {
    return AppWidget.navTitleBar(
      bgColor: Colors.white,
      childWidget: Padding(
        padding: const EdgeInsets.only(left: 20, top: 30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                ClipRRect(
                    borderRadius: BorderRadius.circular(imgWidth / 2.0),
                    child:  Provider.profilePhoto.isEmpty
                        ? Container()
                        : Image.network(
                            Provider.profilePhoto,
                            width: imgWidth,
                            height: imgWidth,
                          )),
                const SizedBox(
                  width: 10,
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '欢迎',
                      style:
                          TextStyle(color: ThemeColor.textColor3, fontSize: 13),
                    ),
                    Text(
                      Provider.nickName,
                      style:
                          TextStyle(color: ThemeColor.textColor3, fontSize: 22),
                    ),
                  ],
                ),
              ],
            ),
            const SizedBox(
              height: 59,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 13),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '你是',
                    style: TextStyle(
                      fontSize: 16,
                      color: ThemeColor.textColor3,
                    ),
                  ),
                  const SizedBox(
                    height: 14,
                  ),
                  Container(
                    margin: const EdgeInsets.only(left: 0, right: 22),
                    child: GetBuilder(
                        id: completeV4Controller.sexViewId,
                        builder: (ProfileCompleteV4Controller controller) {
                          return Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              getFemaleView(controller.sexList.first),
                              getFemaleView(controller.sexList.last),
                            ],
                          );
                        }),
                  ),
                  const SizedBox(
                    height: 20,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(right: 31),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '生日',
                          style: TextStyle(
                            fontSize: 16,
                            color: ThemeColor.textColor3,
                          ),
                        ),
                        GetBuilder(
                            id: completeV4Controller.birthViewId,
                            builder: (ProfileCompleteV4Controller controller) {
                              return GestureDetector(
                                onTap: () {
                                  DatePicker.showDatePicker(context,
                                      showTitleActions: true,
                                      minTime: DateTime(1970, 01, 01, 0, 0),
                                      maxTime: DateTime(2005, 01, 01),
                                      onConfirm: (date) {
                                    controller.dateStr = formatDate(
                                        date, [yyyy, '-', mm, '-', dd]);
                                    controller.update([controller.birthViewId]);
                                  },
                                      currentTime:
                                          DateTime.tryParse("2005-01-01"),
                                      locale: LocaleType.zh);
                                },
                                child: Text(
                                  controller.dateStr.isEmpty
                                      ? '点击设置'
                                      : controller.dateStr,
                                  style: TextStyle(
                                    fontSize: 16,
                                    color: ThemeColor.hint777777Color,
                                  ),
                                ),
                              );
                            }),
                      ],
                    ),
                  ),
                  const SizedBox(
                    height: 14,
                  ),
                ],
              ),
            ),
            Container(
              height: 1,
              color: const Color(0xFFF0F0F0),
            ),
            const SizedBox(
              height: 124,
            ),
            GestureDetector(
              onTap: () {
                completeV4Controller.saveInformation();
              },
              child: Container(
                margin: const EdgeInsets.symmetric(horizontal: 43),
                height: 50,
                decoration: BoxDecoration(
                    color: ThemeColor.appColor,
                    borderRadius: BorderRadius.circular(25)),
                alignment: Alignment.center,
                child: const Text(
                  '保存',
                  style: TextStyle(color: Colors.white, fontSize: 18),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget getFemaleView(SexInfoModel manModel) {
    // --性别 1-男 2-女
    String iconStr = manModel.sex == 1
        ? 'images/scan/man_sex_icon.png'
        : 'images/scan/woman_sex_icon.png';
    String sexStr = manModel.nameStr;
    Color sexColor =
        manModel.isChose ? ThemeColor.appColor : ThemeColor.textColor3;
    return GestureDetector(
      onTap: () {
        if (manModel.isChose) {
          return;
        }
        for (SexInfoModel element in completeV4Controller.sexList) {
          if (element.nameStr == manModel.nameStr) {
            element.isChose = true;
          } else {
            element.isChose = false;
          }
        }
        completeV4Controller.update([completeV4Controller.sexViewId]);
      },
      child: Container(
        width: 137,
        height: 120,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(10),
            color: const Color(0xFFF9F8FD)),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image.asset(
              iconStr,
              width: 27,
              color: sexColor,
            ),
            const SizedBox(
              height: 15,
            ),
            Text(
              sexStr,
              style: TextStyle(color: ThemeColor.textColor3, fontSize: 16),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    completeV4Controller.initSexData();
  }
}
